$(function () {

    // 表单提交
    $("#submit").click(function () {
        let topic = $("#topic").val();
        let content = $("#content").val();
        let contact = $("#contact").val();
        let email = $("#email").val();
        $.ajax({
            url: "/mail/process",
            type: "post",
            data: {
                topic: topic,
                content: content,
                contact: contact,
                email: email
            },
            dataType: 'json',
            success: function (data) {
                if (data.status === 200) {
                    alert(data.data);
                }
            },
            error: function (e) {
                console.log(JSON.stringify(e));
                alert("错误！！");
            }
        });
    });

     let imgUp = $(".imageUp");

    imgUp.files = [];
    console.log(" $(\".imageUp\").files: "+  imgUp.files.length);

    function handleFileSelect(evt) {
        let files = evt.target.files;
        console.log("----:"+JSON.stringify(files));
        console.log("----:"+files.length);
        for (let i = 0, f; f = files[i]; i++) {
            // 过滤掉非图片类型文件
            if (!f.type.match('image.*')) {
                continue;
            }
            // 过滤掉重复上传的图片
            let tip = false;
            for (let j = 0; j < imgUp.files.length; j++) {
                if (imgUp.files[j].name === f.name) {
                    tip = true;
                    console.log("---true-:"+tip);
                    break;
                }
            }
            if (!tip) {
                // 图片文件绑定到容器元素上
                imgUp.files.push(f);
                console.log("---file-:"+f.name);
                let reader = new FileReader();
                reader.onload = (function (theFile) {
                    return function (e) {
                        // 预览上传图片
                        let oDiv =$('<div></div>');
                        oDiv.addClass('img-thumb img-item col-md-2');
                        // 向图片容器里添加元素
                        console.log("e.target.result: "+e.target.result);
                        oDiv.append('<img class="thumb-icon" src="' + e.target.result + '" />' +
                            '<img class="img-remove" src="/image/remove.png"/>');

                        imgUp.prepend(oDiv, $("#add_img"));
                    };
                })(f);

                reader.readAsDataURL(f);
            }
        }
    }

    $('#imgUploadInput').change(handleFileSelect);


});